<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>Python的垃圾回收机制 | Depicter</title><meta name="author" content="fanhao"><meta name="copyright" content="fanhao"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="ffffff"><meta name="description" content="Python垃圾回收1. 引用计数器1.1 环状双向链表 refchain在Python程序中, 创建的任何对象都会放在refchain中 123name &#x3D; &amp;#x27;李白&amp;#x27;age &#x3D; 19hobby &#x3D; [&amp;#x27;coding&amp;#x27;, &amp;#x27;eating&amp;#x27;] 123456789# 内部会创建一些数据 [上一个对象, 下一个对象, 类型, 引用个数]name">
<meta property="og:type" content="article">
<meta property="og:title" content="Python的垃圾回收机制">
<meta property="og:url" content="https://crappier.github.io/2023/04/10/Python%E7%9A%84%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E6%9C%BA%E5%88%B6/index.html">
<meta property="og:site_name" content="Depicter">
<meta property="og:description" content="Python垃圾回收1. 引用计数器1.1 环状双向链表 refchain在Python程序中, 创建的任何对象都会放在refchain中 123name &#x3D; &amp;#x27;李白&amp;#x27;age &#x3D; 19hobby &#x3D; [&amp;#x27;coding&amp;#x27;, &amp;#x27;eating&amp;#x27;] 123456789# 内部会创建一些数据 [上一个对象, 下一个对象, 类型, 引用个数]name">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://crappier.github.io/img/cover/cover_1.jpg">
<meta property="article:published_time" content="2023-04-10T13:19:48.000Z">
<meta property="article:modified_time" content="2023-04-10T14:35:03.061Z">
<meta property="article:author" content="fanhao">
<meta property="article:tag" content="Python">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://crappier.github.io/img/cover/cover_1.jpg"><link rel="shortcut icon" href="/img/dinosaur.png"><link rel="canonical" href="https://crappier.github.io/2023/04/10/Python%E7%9A%84%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E6%9C%BA%E5%88%B6/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":false,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '天',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Python的垃圾回收机制',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-04-10 22:35:03'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', 'ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">10</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">5</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于笔者</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg fixed" id="page-header" style="background-image: url('/img/cover/cover_1.jpg')"><nav id="nav"><span id="blog-info"><a href="/" title="Depicter"><span class="site-name">Depicter</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于笔者</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Python的垃圾回收机制</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-04-10T13:19:48.000Z" title="发表于 2023-04-10 21:19:48">2023-04-10</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-04-10T14:35:03.061Z" title="更新于 2023-04-10 22:35:03">2023-04-10</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Python的垃圾回收机制"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Python垃圾回收"><a href="#Python垃圾回收" class="headerlink" title="Python垃圾回收"></a>Python垃圾回收</h1><h2 id="1-引用计数器"><a href="#1-引用计数器" class="headerlink" title="1. 引用计数器"></a>1. 引用计数器</h2><h3 id="1-1-环状双向链表-refchain"><a href="#1-1-环状双向链表-refchain" class="headerlink" title="1.1 环状双向链表 refchain"></a>1.1 环状双向链表 refchain</h3><p>在Python程序中, 创建的任何对象都会放在refchain中</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">name = <span class="string">&#x27;李白&#x27;</span></span><br><span class="line">age = <span class="number">19</span></span><br><span class="line">hobby = [<span class="string">&#x27;coding&#x27;</span>, <span class="string">&#x27;eating&#x27;</span>]</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 内部会创建一些数据 [上一个对象, 下一个对象, 类型, 引用个数]</span></span><br><span class="line">name = <span class="string">&#x27;李白&#x27;</span> </span><br><span class="line">new = name</span><br><span class="line"></span><br><span class="line"><span class="comment"># 内部会创建一些数据 [上一个对象, 下一个对象, 类型, 引用个数, val=19]</span></span><br><span class="line">age = <span class="number">19</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 内部会创建一些数据 [上一个对象, 下一个对象, 类型, 引用个数, items=元素, 元素个数]</span></span><br><span class="line">hobby = [<span class="string">&#x27;coding&#x27;</span>, <span class="string">&#x27;eating&#x27;</span>]</span><br></pre></td></tr></table></figure>
<p>在C源码中, 如何体现每个对象都有相同的值:</p>
<ul>
<li>PyObject结构体(4个值)</li>
</ul>
<p>有多个元素组成的对象:</p>
<ul>
<li>PyObject结构体(4个值) + ob_size</li>
</ul>
<h3 id="1-2-类型封装结构体"><a href="#1-2-类型封装结构体" class="headerlink" title="1.2 类型封装结构体"></a>1.2 类型封装结构体</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">data = <span class="number">3.14</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 内部会创建:</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    _ob_next = refchain中的上一个对象</span></span><br><span class="line"><span class="string">    _ob_prev = refchain中的下一个对象</span></span><br><span class="line"><span class="string">    ob_refcnt = 1</span></span><br><span class="line"><span class="string">    ob_type = float</span></span><br><span class="line"><span class="string">    ob_fval = 3.14</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
<h3 id="1-3-引用计数器"><a href="#1-3-引用计数器" class="headerlink" title="1.3 引用计数器"></a>1.3 引用计数器</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">v1 = <span class="number">3.14</span></span><br><span class="line">v2 = <span class="number">999</span></span><br><span class="line">v3 = (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>)</span><br></pre></td></tr></table></figure>
<p>当Python运行时, 会根据数据类型的不同找到其对应的结构体, 根据结构体的字段类进行创建相关的数据, 然后将对象添加到refchain的双向链表中</p>
<p>在C源码中有两个关键的结构体: PyObject, PyVarObject</p>
<p>每个对象中都有ob_refcnt, 就是引用计数器, 值默认为1, 当有其他变量引用对象时, 引用计数器就会发生变化</p>
<ul>
<li>引用<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">999</span> </span><br><span class="line">b = a</span><br></pre></td></tr></table></figure></li>
<li>删除引用<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">999</span></span><br><span class="line">b = a</span><br><span class="line"><span class="comment"># b变量删除, b对应对象引用计数器-1</span></span><br><span class="line"><span class="keyword">del</span> b</span><br><span class="line"><span class="comment"># a变量删除, a对应对象引用计数器-1</span></span><br><span class="line"><span class="keyword">del</span> a</span><br><span class="line"></span><br><span class="line"><span class="comment"># 当一个对象的引用计数器为0时, 意味着没有人使用这个对象了, 这个对象就是垃圾, 进行垃圾回收</span></span><br><span class="line"><span class="comment"># 回收: </span></span><br><span class="line"><span class="comment"># 1. 对象从refchain链表移除</span></span><br><span class="line"><span class="comment"># 2. 将对象销毁, 内存归还</span></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="1-4-循环引用问题"><a href="#1-4-循环引用问题" class="headerlink" title="1.4 循环引用问题"></a>1.4 循环引用问题</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># refchain中创建一个列表对象, 由于v1=对象, 所以列表对象引用计数器为1</span></span><br><span class="line">v1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"><span class="comment"># refachin中再创建一个列表对象, 由于v2=对象, 所以列表对象引用计数器为1</span></span><br><span class="line">v2 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># v2追加到v1中, 则v2对应的[1, 2, 3]对象的引用计数器加一, 为2</span></span><br><span class="line">v1.append(v2)</span><br><span class="line"><span class="comment"># v1追加到v2中, 则v1对于的[1, 2, 3]对象的引用计数器加一, 为2</span></span><br><span class="line">v2.append(v1)</span><br><span class="line"></span><br><span class="line"><span class="keyword">del</span> v1  <span class="comment"># 引用计数器-1</span></span><br><span class="line"><span class="keyword">del</span> v2  <span class="comment"># 引用计数器-1</span></span><br></pre></td></tr></table></figure>
<h2 id="2-标记清除"><a href="#2-标记清除" class="headerlink" title="2. 标记清除"></a>2. 标记清除</h2><blockquote>
<p>目的: 为了解决引用计数器循环引用的不足</p>
</blockquote>
<p>实现: 在Python的底层再维护一个链表, 链表中专门存放那些可能存在循环引用的对象</p>
<ul>
<li>list</li>
<li>tuple</li>
<li>dict</li>
<li>set</li>
</ul>
<p>在Python的内部<code>某种情况下</code>触发, 扫描可以嗯存在循环引用的链表中的每一个元素, 检查是否有循环引用, 如果有则让双方的引用计数器-1; 如果发现是, 则表明是垃圾, 进行回收</p>
<p>问题:</p>
<ul>
<li>什么时候扫描?</li>
<li>可能存在循环引用的链表扫描代价大, 耗时久</li>
</ul>
<h2 id="3-分代回收"><a href="#3-分代回收" class="headerlink" title="3. 分代回收"></a>3. 分代回收</h2><p>将肯存在循环引用的对象维护称3个链表</p>
<ul>
<li>0代: 0代中对象个数达到700个扫描一次</li>
<li>1代: 0代扫描10次, 则1代扫描一次</li>
<li>2代: 1代扫描10次, 则2代扫描一次</li>
</ul>
<h2 id="4-小结"><a href="#4-小结" class="headerlink" title="4. 小结"></a>4. 小结</h2><p>在Python中维护了一个refchain的双向环状链表, 这个链表中存储程序创建的所有对象, 每种类型的对象中都有一个ob_refcnt引用计数器的值, 引用个数+1, -1, 最后当引用计数器变为0时, 进进行垃圾回收(对象销毁, refchain中移出)</p>
<p>但是, 在Python中对于那些可以有多格元素组成的对象可能会存在循环引用的问题, 为了解决这个问题Python又引入了标记清除和分代回收, 在其内部分成4个链表</p>
<ul>
<li>refchain</li>
<li>2代, 10次</li>
<li>1代, 10次</li>
<li>0代, 700个</li>
</ul>
<p>在源码内部当达到各自的阈值时, 就会触发扫描链表进行标记清除的动作(有循环则各自-1)</p>
<blockquote>
<p>但是, 在Python源码内部在上述流程中提出了优化机制</p>
</blockquote>
<h2 id="5-Python缓存"><a href="#5-Python缓存" class="headerlink" title="5. Python缓存"></a>5. Python缓存</h2><h3 id="5-1-池-int"><a href="#5-1-池-int" class="headerlink" title="5.1 池(int)"></a>5.1 池(int)</h3><p>为了避免重复创建和销毁一些常见对象, 维护池</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 启动解释器时, Python内部帮我们创建: -5, -4, ... 257</span></span><br><span class="line">v1 = <span class="number">7</span>  <span class="comment"># 内存不会开辟内存, 直接去池中获取</span></span><br><span class="line">v2 = <span class="number">9</span>  <span class="comment"># 内存不会开辟内存, 直接去池中获取</span></span><br><span class="line">v3 = <span class="number">9</span>  <span class="comment"># 内存不会开辟内存, 直接去池中获取</span></span><br><span class="line"></span><br><span class="line">v4 = <span class="number">999</span></span><br><span class="line">v5 = <span class="number">1234</span></span><br><span class="line">v6 = <span class="number">666</span></span><br></pre></td></tr></table></figure>
<h3 id="5-2-free-list-float-str-list"><a href="#5-2-free-list-float-str-list" class="headerlink" title="5.2 free_list(float, str, list)"></a>5.2 free_list(float, str, list)</h3><p>当一个对象的引用计数器为0时, 按理说应该回收, 但是内部不会回收, 而是将对象添加到free_list链表中当做缓存. 以后再去创建对象时, 不再重新开辟内存, 而是直接使用free_list</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 开辟内存, 并存储到refchain中</span></span><br><span class="line">v1 = <span class="number">3.14</span></span><br><span class="line"><span class="comment"># refchain中移除, 将对象添加到free_list中</span></span><br><span class="line"><span class="keyword">del</span> v1</span><br><span class="line"><span class="comment"># 不会重新开辟内存, 去free_list中获取对象, 对象内部数据初始化, 再放到refchain中</span></span><br><span class="line">v9 = <span class="number">999</span></span><br></pre></td></tr></table></figure>
<ul>
<li>float: 维护的free_list链表中最多可缓存100个<code>float对象</code></li>
<li>str: 维护unicode_latin1[256]链表, 内部将所有的<code>ascii字符</code>缓存起来</li>
<li>list: 维护的free_list数组最多可缓存80个<code>list对象</code></li>
<li>tuple: 维护一个free_list数组, 且数组容量为20, 数组中的元素可以是链表, 且每个链表最多可容纳2000个元组对象<ul>
<li>free_list &#x3D; [0, 1, 2, 3, …19]</li>
<li>free_list[0] : 存储空元组</li>
<li>free_list[1] : 存储1个元素的元组</li>
<li>free_list[2] : 存储2个元素的元组</li>
<li>…</li>
<li>free_list[19] : 存储19个元素的元组</li>
</ul>
</li>
<li>dict: 维护的free_list数组最多可缓存80个dict对象</li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://crappier.github.io">fanhao</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://crappier.github.io/2023/04/10/Python%E7%9A%84%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E6%9C%BA%E5%88%B6/">https://crappier.github.io/2023/04/10/Python%E7%9A%84%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E6%9C%BA%E5%88%B6/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://crappier.github.io" target="_blank">Depicter</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Python/">Python</a></div><div class="post_share"><div class="social-share" data-image="/img/cover/cover_1.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/04/13/%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%85%A5%E9%97%A8/" title="大数据入门"><img class="cover" src="/img/cover/cover_4.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">大数据入门</div></div></a></div><div class="next-post pull-right"><a href="/2023/04/10/%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%8D%9A%E5%AE%A2/" title="第一个博客"><img class="cover" src="/img/cover/cover_2.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">第一个博客</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2023/05/20/Django%E5%BC%80%E5%8F%91%E7%9F%A5%E8%AF%86%E7%82%B9/" title="Django开发知识点"><img class="cover" src="/img/cover/cover_6.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-20</div><div class="title">Django开发知识点</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">fanhao</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">10</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">5</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/fanhao0416"><i class="fab fa-gitee"></i><span>联系我</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/fanhao0416" target="_blank" title="Gitee"><i class="fab fa-git" style="color: #DC143C;"></i></a><a class="social-icon" href="mailto:2039216364@qq.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a><a class="social-icon" href="http://wpa.qq.com/msgrd?v=3&amp;uin=2039216364&amp;site=qq&amp;menu=yes" target="_blank" title="QQ"><i class="fab fa-qq" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">沧海易幻莫同，世人皆平庸</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Python%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6"><span class="toc-number">1.</span> <span class="toc-text">Python垃圾回收</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E5%BC%95%E7%94%A8%E8%AE%A1%E6%95%B0%E5%99%A8"><span class="toc-number">1.1.</span> <span class="toc-text">1. 引用计数器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-%E7%8E%AF%E7%8A%B6%E5%8F%8C%E5%90%91%E9%93%BE%E8%A1%A8-refchain"><span class="toc-number">1.1.1.</span> <span class="toc-text">1.1 环状双向链表 refchain</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-%E7%B1%BB%E5%9E%8B%E5%B0%81%E8%A3%85%E7%BB%93%E6%9E%84%E4%BD%93"><span class="toc-number">1.1.2.</span> <span class="toc-text">1.2 类型封装结构体</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-%E5%BC%95%E7%94%A8%E8%AE%A1%E6%95%B0%E5%99%A8"><span class="toc-number">1.1.3.</span> <span class="toc-text">1.3 引用计数器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-4-%E5%BE%AA%E7%8E%AF%E5%BC%95%E7%94%A8%E9%97%AE%E9%A2%98"><span class="toc-number">1.1.4.</span> <span class="toc-text">1.4 循环引用问题</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E6%A0%87%E8%AE%B0%E6%B8%85%E9%99%A4"><span class="toc-number">1.2.</span> <span class="toc-text">2. 标记清除</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E5%88%86%E4%BB%A3%E5%9B%9E%E6%94%B6"><span class="toc-number">1.3.</span> <span class="toc-text">3. 分代回收</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E5%B0%8F%E7%BB%93"><span class="toc-number">1.4.</span> <span class="toc-text">4. 小结</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-Python%E7%BC%93%E5%AD%98"><span class="toc-number">1.5.</span> <span class="toc-text">5. Python缓存</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-1-%E6%B1%A0-int"><span class="toc-number">1.5.1.</span> <span class="toc-text">5.1 池(int)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-2-free-list-float-str-list"><span class="toc-number">1.5.2.</span> <span class="toc-text">5.2 free_list(float, str, list)</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2024/03/12/Nmap%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/" title="Nmap使用指南"><img src="/img/cover/cover_2.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Nmap使用指南"/></a><div class="content"><a class="title" href="/2024/03/12/Nmap%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/" title="Nmap使用指南">Nmap使用指南</a><time datetime="2024-03-12T02:38:29.000Z" title="发表于 2024-03-12 10:38:29">2024-03-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2024/03/10/TCP%E5%8D%8F%E8%AE%AE/" title="TCP协议"><img src="/img/cover/cover_9.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="TCP协议"/></a><div class="content"><a class="title" href="/2024/03/10/TCP%E5%8D%8F%E8%AE%AE/" title="TCP协议">TCP协议</a><time datetime="2024-03-10T08:15:52.000Z" title="发表于 2024-03-10 16:15:52">2024-03-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2024/03/06/%E5%B8%B8%E7%94%A8%E7%BD%91%E7%BB%9C%E5%AE%89%E5%85%A8%E8%AE%BE%E5%A4%87/" title="常用网络安全设备"><img src="/img/cover/cover_6.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="常用网络安全设备"/></a><div class="content"><a class="title" href="/2024/03/06/%E5%B8%B8%E7%94%A8%E7%BD%91%E7%BB%9C%E5%AE%89%E5%85%A8%E8%AE%BE%E5%A4%87/" title="常用网络安全设备">常用网络安全设备</a><time datetime="2024-03-06T00:58:05.000Z" title="发表于 2024-03-06 08:58:05">2024-03-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2024/03/06/%E6%B8%97%E9%80%8F%E6%B5%8B%E8%AF%95/" title="渗透测试"><img src="/img/cover/cover_9.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="渗透测试"/></a><div class="content"><a class="title" href="/2024/03/06/%E6%B8%97%E9%80%8F%E6%B5%8B%E8%AF%95/" title="渗透测试">渗透测试</a><time datetime="2024-03-06T00:57:01.000Z" title="发表于 2024-03-06 08:57:01">2024-03-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2024/03/06/OWASP-TOP-10/" title="OWASP-TOP-10"><img src="/img/cover/cover_2.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="OWASP-TOP-10"/></a><div class="content"><a class="title" href="/2024/03/06/OWASP-TOP-10/" title="OWASP-TOP-10">OWASP-TOP-10</a><time datetime="2024-03-06T00:55:41.000Z" title="发表于 2024-03-06 08:55:41">2024-03-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2025 By fanhao</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="true" data-click="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>